﻿using System;
using System.Collections.Generic;
using System.IO;
using Db4objects.Db4o;
using NmbpProject2.DboClasses;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

namespace NmbpProject2
{
    /// <summary>
    /// db4o connection manager
    /// </summary>
    public static class DataConfig
    {
        private static string _dbFileName = "db.yap";
        public static IObjectContainer db { get; set; }

        public static string DbFileName
        {
            get { return _dbFileName; }
        }

        public static void openConnection()
        {
            bool fileExists = File.Exists(DataConfig.DbFileName);
            db = Db4oFactory.OpenFile(DataConfig.DbFileName);
            if (!fileExists)
            {
                setDefaultData();
            }
        }

        public static void closeConnection()
        {
            db.Close();
        }

        private static void setDefaultData()
        {
            // People
            Person lana = new Person("Lana", "Geric", 1234567891234);
            DataConfig.db.Store(lana);
            Person marko = new Person("Marko", "Suveljak-Vujnovic", 1234567891235);
            DataConfig.db.Store(marko);
            Person andrija = new Person("Andrija", "Slezak", 1234567891237);
            DataConfig.db.Store(andrija);
            Person martin = new Person("Martin", "Vrkljan", 1234567891238);
            DataConfig.db.Store(martin);

            // Programming Languages
            ProgLanguage cpp = new ProgLanguage("C++");
            DataConfig.db.Store(cpp);
            ProgLanguage cSharp = new ProgLanguage("C#");
            DataConfig.db.Store(cSharp);
            ProgLanguage c = new ProgLanguage("C");
            DataConfig.db.Store(c);
            ProgLanguage php = new ProgLanguage("PHP");
            DataConfig.db.Store(php);
            ProgLanguage perl = new ProgLanguage("Perl");
            DataConfig.db.Store(perl);
            ProgLanguage python = new ProgLanguage("Python");
            DataConfig.db.Store(python);

            // Categories
            Category extreme = new Category("Extreme programming");
            DataConfig.db.Store(extreme);
            Category games = new Category("Game programming");
            DataConfig.db.Store(games);
            Category ai = new Category("Artificial inteligence programming");
            DataConfig.db.Store(ai);
            Category ood = new Category("Object oriented design");
            DataConfig.db.Store(ood);

            // Competitions
            Competition croatianComp = new Competition(
                "Croatian programming competition",
                DateTime.Now.AddDays(90),
                new List<Category>(new[] { extreme, ood }),
                new List<ProgLanguage>(new[] { cpp, cSharp, c })
                );

            Competition worldComp = new Competition(
                "World programming competition",
                DateTime.Now.AddDays(120),
                new List<Category>(new[] { extreme, ood, games, ai }),
                new List<ProgLanguage>(new[] { cpp, cSharp, c, php, perl, python })
                );

            //Add applications
            CompApplication appLana = new CompApplication(
                lana,
                new List<ProgLanguage>(new[] { cSharp }),
                DateTime.Today,
                new List<Category>(new[] { ood })
                );
            croatianComp.addApplication(appLana);

            croatianComp.addApplication(
                new CompApplication(
                    martin,
                    new List<ProgLanguage>(new[] { cSharp }),
                    DateTime.Today.AddDays(1),
                    new List<Category>(new[] { ood })
                    )
                );
            worldComp.addApplication(
                new CompApplication(
                    marko,
                    new List<ProgLanguage>(new[] { cSharp, cpp }),
                    DateTime.Today,
                    new List<Category>(new[] { ood, games })
                    )
                );
            worldComp.addApplication(
                new CompApplication(
                    andrija,
                    new List<ProgLanguage>(new[] { cSharp }),
                    DateTime.Today,
                    new List<Category>(new[] { ood, extreme })
                    )
                );
            DataConfig.db.Store(worldComp);
            DataConfig.db.Store(croatianComp);
        }
    }

    public enum dbtype
    {
        Db40,
        Oracle,
        Xml,
        GeoSpatial
    }

    public static class DbConfig
    {
        public static dbtype DBTYPE = dbtype.Db40;
    }

    /// <summary>
    /// klasa koja radi s vezom na oracle bazu
    /// tu se nalazi i connection string
    /// </summary>
    public class OracleConnectionClass
    {
        public OracleConnection conn = new OracleConnection();

        public string openConnection()
        {
            try
            {
                string dbcs = "Data Source=XE;User Id=APP;Password=nmbp;";
                conn.ConnectionString = dbcs;
                conn.Open();

            }
            catch (OracleException e)
            {

                return e.Message;
            }

            return conn.State.ToString();
        }

        public void closeConnection()
        {
            conn.Close();
        }
    }
}